﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class CustomBuild : MonoBehaviour {

	static void BuildiOS() {
		string[] args = System.Environment.GetCommandLineArgs ();

		Debug.Log ("================================== Build iOS ======================================");

		string outputDir = getArg (args, "o");
		string appId = getArg (args, "appid", PlayerSettings.applicationIdentifier);
		string appName = getArg (args, "name", PlayerSettings.productName);
		string appVersion = getArg(args, "version", PlayerSettings.bundleVersion);

		Debug.Log ("Output directory: " + outputDir);
		Debug.Log ("App ID: " + appId);
		Debug.Log ("App Name: " + appName);
		Debug.Log ("App version:" + appVersion);

		if (outputDir != null) {
			PlayerSettings.applicationIdentifier = appId;
			PlayerSettings.productName = appName;
			PlayerSettings.bundleVersion = appVersion;

			string[] scenes = getScenes ();

			printScenes (scenes);

			BuildPipeline.BuildPlayer (scenes, outputDir, BuildTarget.iOS, BuildOptions.CompressWithLz4);
		} else {
			Debug.Log("Fail, must specific output dir");
		}

		Debug.Log ("================================ Build iOS Done ====================================");
	}

	static void BuildAndroid() {
		string[] args = System.Environment.GetCommandLineArgs ();
		
		Debug.Log ("================================== Build Android ======================================");
		
		string outputDir = getArg (args, "o");
		string appId = getArg (args, "appid", PlayerSettings.applicationIdentifier);
		string appName = getArg (args, "name", PlayerSettings.productName);
		string appVersion = getArg(args, "version", PlayerSettings.bundleVersion);

		
		Debug.Log ("Output directory: " + outputDir);
		Debug.Log ("App ID: " + appId);
		Debug.Log ("App Name: " + appName);
		Debug.Log ("App version:" + appVersion);
		
		if (outputDir != null) {
			PlayerSettings.applicationIdentifier = appId;
			PlayerSettings.productName = appName;
			PlayerSettings.bundleVersion = appVersion;
			
			string[] scenes = getScenes ();
			
			printScenes (scenes);
			
			BuildPipeline.BuildPlayer (scenes, outputDir, BuildTarget.Android, BuildOptions.CompressWithLz4);
		} else {
			Debug.Log("Fail, must specific output dir");
		}
		
		Debug.Log ("================================ Build Android Done ====================================");
	}

	static void BuildXiaoMi() {
		string[] args = System.Environment.GetCommandLineArgs ();

		Debug.Log ("================================== Build XiaoMi ======================================");

		string outputDir = getArg (args, "o");
		string appId = getArg (args, "appid", PlayerSettings.applicationIdentifier);
		string appName = getArg (args, "name", PlayerSettings.productName);
		string appVersion = getArg(args, "version", PlayerSettings.bundleVersion);
		string scriptSymbols = getArg (args, "symbols");

		Debug.Log ("Output directory: " + outputDir);
		Debug.Log ("App ID: " + appId);
		Debug.Log ("App Name: " + appName);
		Debug.Log ("App version:" + appVersion);

		if (outputDir != null) {
			PlayerSettings.applicationIdentifier = appId;
			PlayerSettings.productName = appName;
			PlayerSettings.bundleVersion = appVersion;
			PlayerSettings.SetScriptingDefineSymbolsForGroup (BuildTargetGroup.Android, "CROSS_PLATFORM_INPUT;MOBILE_INPUT;UNITY_POST_PROCESSING_STACK_V2;TALKING_DATA;XIAOMI");

			string[] scenes = getScenes ();

			printScenes (scenes);

			BuildPipeline.BuildPlayer (scenes, outputDir, BuildTarget.Android, BuildOptions.AcceptExternalModificationsToPlayer);
		} else {
			Debug.Log("Fail, must specific output dir");
		}

		Debug.Log ("================================ Build XiaoMi Done ====================================");
	}

	static void printScenes(string[] scenes) {
		string sceneStr = "BuildScenes:\n";

		foreach (string scene in scenes) {
			sceneStr += " " + scene + "\n";
		}

		Debug.Log (sceneStr);
	}

	static string[] getScenes() {
		List<string> scenes = new List<string> ();

		foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes) {
			if (scene.enabled) {
				scenes.Add(scene.path);
			}
		}

		return scenes.ToArray ();
	}

	static string getArg(string[] args, string name, string def = null) {
		string arg = def;

		for (int i = 0; i < args.Length; ++i) {
			if (args[i] == "-" + name) {
				if (i + 1 < args.Length) {
					arg = args[i+1];
					break;
				}
			}
		}

		return arg;
	}
}
